﻿Imports System.Data
Imports Cls_Common
Imports VSModel
Partial Class Control_VBPQ_List
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            'xu ly jquery khi post back
            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "jqueryEnable", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "jqueryEnable", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If
            ShowData()
            BindToGrid(1, txtTitleFilter.Text.Trim(), txtCodeFilter.Text.Trim(), drpType.SelectedValue, 3, drpPromulgator.SelectedValue, drpSigner.SelectedValue, txtStartPublish.Text.Trim(), txtEndPublish.Text.Trim())
        End If
    End Sub

    Protected Sub ShowData()
        Using data As New VSEntities
            'Show loai van ban
            Dim lvb = (From ck In data.VBPQLoaiVanBans Select ck).ToList
            If Not lvb Is Nothing Then
                drpType.DataSource = lvb
                drpType.DataTextField = "LoaiVanBan"
                drpType.DataValueField = "Id"
                drpType.DataBind()
                drpType.Items.Add(New ListItem("---Tất cả---", 0))
                drpType.SelectedValue = 0
            End If
            'Show co quan ban hanh da ton tai
            Dim cqbh = (From ck In data.VBPQCoQuanBHs Select ck).ToList
            If Not cqbh Is Nothing Then
                drpPromulgator.DataSource = cqbh
                drpPromulgator.DataTextField = "CoQuanBH"
                drpPromulgator.DataValueField = "Id"
                drpPromulgator.DataBind()
                drpPromulgator.Items.Add(New ListItem("---Tất cả---", 0))
                drpPromulgator.SelectedValue = 0
            End If
            'Show nguoi ky da ton tai                
            Dim nk = (From ck In data.VBPQNguoiKies Select ck).ToList
            If Not nk Is Nothing Then
                drpSigner.DataSource = nk
                drpSigner.DataTextField = "NguoiKy"
                drpSigner.DataValueField = "Id"
                drpSigner.DataBind()
                drpSigner.Items.Add(New ListItem("---Tất cả---", 0))
                drpSigner.SelectedValue = 0
            End If
        End Using
    End Sub
    Private Sub BindToGrid(Optional ByVal iPage As Integer = 1, Optional ByVal strTitle As String = "", Optional ByVal strCode As String = "", Optional ByVal intType As Integer = -1, Optional ByVal intStatus As Integer = -1, Optional ByVal intCQBH As Integer = -1, Optional ByVal intSigner As Integer = 0, Optional ByVal strStartPublish As String = "", Optional ByVal strEndPublish As String = "")
        Using data As New VSEntities
            Dim arrSearch() As String = {iPage.ToString, _
                                         strTitle, _
                                         strCode, _
                                         intType.ToString, _
                                         intStatus.ToString, _
                                         intCQBH.ToString, _
                                         intSigner.ToString, _
                                         strStartPublish, _
                                         strEndPublish _
                                       }
            ViewState("search") = arrSearch
            If strStartPublish = "" Then
                strStartPublish = "01/01/1945"
            End If
            If strEndPublish = "" Then
                strEndPublish = "01/01/8888"
            End If
            'So ban ghi muon the hien tren trang                       
            Dim intOrderBy As Int32 = drpSort.SelectedValue
            'Dim p = (From a In data.VBPQDocuments Select a Order By a.NgayBanHanh).Skip((iPage - 1) * 10).Take(10)

            Dim p As IList(Of uspVBPQDocumentSelectAll_Result) = data.uspVBPQDocumentSelectAll(IIf(String.IsNullOrEmpty(strTitle.Trim) = True, Nothing, strTitle.Trim), IIf(String.IsNullOrEmpty(strCode.Trim) = True, Nothing, strCode.Trim), IIf(intType = 0, Nothing, intType), IIf(intStatus = 0, Nothing, intStatus), IIf(intCQBH = 0, Nothing, intCQBH), IIf(intSigner = 0, Nothing, intSigner), IIf(String.IsNullOrEmpty(strStartPublish) = True, Nothing, StringToDate(strStartPublish)), IIf(String.IsNullOrEmpty(strEndPublish) = True, Nothing, StringToDate(strEndPublish)), intOrderBy, iPage, 1000).ToList


            Dim strKey_Name() As String = {"Id", "TrichYeu", "StatusId", "SoKyHieu"}
            'Tong so ban ghi
            If p.Count > 0 Then
                hidCount.Value = p.FirstOrDefault.Total()
                Create_Pager(hidCount.Value, iPage, 50, 50)
            Else
                hidCount.Value = 0
            End If


            'hien thi theo loai van ban
            Dim _luat = (From q In p Where q.LoaiVanBanId = 1 Select q).ToList
            If Not _luat Is Nothing Then
                With grdShowLuat
                    .DataKeyNames = strKey_Name
                    .DataSource = _luat
                    .DataBind()
                End With
            End If
            'hien thi theo nghi dinh
            Dim _NghiDinh = (From q In p Where q.LoaiVanBanId = 2 Select q).ToList
            If Not _luat Is Nothing Then
                With grdShowNghiDinh
                    .DataKeyNames = strKey_Name
                    .DataSource = _NghiDinh
                    .DataBind()
                End With
            End If
            'hien thi theo thong tu
            Dim _ThongTu = (From q In p Where q.LoaiVanBanId = 3 Select q).ToList
            If Not _luat Is Nothing Then
                With grdShowThongTu
                    .DataKeyNames = strKey_Name
                    .DataSource = _ThongTu
                    .DataBind()
                End With
            End If
            'hien thi theo quyet dinh
            Dim _QuyetDinh = (From q In p Where q.LoaiVanBanId = 4 Select q).ToList
            If Not _luat Is Nothing Then
                With grdShowQuyetDinh
                    .DataKeyNames = strKey_Name
                    .DataSource = _QuyetDinh
                    .DataBind()
                End With
            End If
            'hien thi theo van ban lien quan
            Dim _VanBanLienQuan = (From q In p Where q.LoaiVanBanId = 5 Select q).ToList
            If Not _luat Is Nothing Then
                With grdShowVanBanLienQuan
                    .DataKeyNames = strKey_Name
                    .DataSource = _VanBanLienQuan
                    .DataBind()
                End With
            End If
        End Using
    End Sub
    Protected Sub grdShowLuat_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdShowLuat.RowDataBound
        If e.Row.RowIndex >= 0 Then
            Dim lblSTT As Label = CType(e.Row.FindControl("lblSTT"), Label)
            lblSTT.Text = CInt((hidCur_Page.Value) - 1).ToString + e.Row.RowIndex + 1
            Dim status As Integer = grdShowLuat.DataKeys(e.Row.RowIndex)("StatusId")
            Dim lnkbtnDelete As LinkButton = CType(e.Row.FindControl("lnkbtnDelete"), LinkButton)
            Dim chkItem As CheckBox = CType(e.Row.FindControl("chkItem"), CheckBox)
            Dim hplTitle As HyperLink = CType(e.Row.FindControl("hplTitle"), HyperLink)
            hplTitle.NavigateUrl = MainDomain & "van-ban-phap-luat/chi-tiet-" & grdShowLuat.DataKeys(e.Row.RowIndex)("Id").ToString & ".html"


            hplTitle.Text = grdShowLuat.DataKeys(e.Row.RowIndex)("TrichYeu").ToString
        End If
    End Sub

    Protected Sub rptPage_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptPage.ItemDataBound
        Dim lnkTitle As LinkButton
        lnkTitle = e.Item.FindControl("lnkTitle")
        'Dim ScriptManager As System.Web.UI.ScriptManager = System.Web.UI.ScriptManager.GetCurrent(Me.Page)
        'ScriptManager.RegisterAsyncPostBackControl(lnkTitle)

        If e.Item.DataItem = hidCur_Page.Value Then
            lnkTitle.Text = "<span class='current'>" & e.Item.DataItem & "</span>"
        Else
            lnkTitle.Text = "<span>" & e.Item.DataItem & "</span>"
        End If
        lnkTitle.ToolTip = e.Item.DataItem
    End Sub
    Sub Create_Pager(ByVal Total_Record As Integer, ByVal Page_Index As Integer, ByVal Page_Size As Integer, ByVal Page2Show As Integer)
        Dim TotalPage As Integer = IIf((Total_Record Mod Page_Size) = 0, Total_Record / Page_Size, Total_Record \ Page_Size + 1)
        'lu lai tong so ban ghi
        hidIndex_page.Value = TotalPage
        'gan lai curPage de set active
        hidCur_Page.Value = Page_Index
        'generate ra left page
        Dim cPageGenerate_left As IEnumerable(Of Integer)
        If Page_Index <= Page2Show Then
            cPageGenerate_left = Enumerable.Range(1, Page_Index)
        Else
            cPageGenerate_left = Enumerable.Range(Page_Index - Page2Show, Page2Show)
        End If
        'generate ra right page
        Dim cPageGenerate_Right As IEnumerable(Of Integer)
        If Page_Index + Page2Show <= TotalPage Then
            cPageGenerate_Right = Enumerable.Range(Page_Index, Page2Show + 1)
        Else
            cPageGenerate_Right = Enumerable.Range(Page_Index, TotalPage - Page_Index + 1)
        End If
        'union 2 range va bind to Grid
        With rptPage
            .DataSource = cPageGenerate_left.Union(cPageGenerate_Right)
            .DataBind()
        End With
    End Sub


    Protected Sub lnkFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFirst.Click
        If hidCur_Page.Value > 1 Then
            hidCur_Page.Value = hidCur_Page.Value - 1
        End If
        BindToGrid(hidCur_Page.Value)
        lnkLast.Enabled = True
        lnkFirst.Enabled = True

        If hidCur_Page.Value = hidIndex_page.Value Then
            lnkLast.Enabled = False
        ElseIf hidCur_Page.Value = 1 Then
            lnkFirst.Enabled = False
        End If

    End Sub
    Protected Sub lnkLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkLast.Click
        hidCur_Page.Value = hidCur_Page.Value + 1
        BindToGrid(hidCur_Page.Value)

        lnkLast.Enabled = True
        lnkFirst.Enabled = True

        If hidCur_Page.Value = hidIndex_page.Value Then
            lnkLast.Enabled = False
        ElseIf hidCur_Page.Value = 1 Then
            lnkFirst.Enabled = False
        End If

    End Sub
    Protected Sub lnkTitle_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim lnkTile As LinkButton = CType(sender, LinkButton)

        BindToGrid(CInt(lnkTile.ToolTip))
        lnkLast.Enabled = True
        lnkFirst.Enabled = True

        If CInt(lnkTile.ToolTip) = hidIndex_page.Value Then
            lnkLast.Enabled = False
        ElseIf CInt(lnkTile.ToolTip) = 1 Then
            lnkFirst.Enabled = False
        End If
    End Sub

    Protected Sub btnFilter_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFilter.Click
        BindToGrid(1, txtTitleFilter.Text.Trim(), txtCodeFilter.Text.Trim(), drpType.SelectedValue, 3, drpPromulgator.SelectedValue, drpSigner.SelectedValue, txtStartPublish.Text.Trim(), txtEndPublish.Text.Trim())
    End Sub

    Protected Sub grdShowNghiDinh_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdShowNghiDinh.RowDataBound
        If e.Row.RowIndex >= 0 Then
            Dim lblSTT As Label = CType(e.Row.FindControl("lblSTT"), Label)
            lblSTT.Text = CInt((hidCur_Page.Value) - 1).ToString + e.Row.RowIndex + 1
            Dim status As Integer = grdShowNghiDinh.DataKeys(e.Row.RowIndex)("StatusId")
            Dim lnkbtnDelete As LinkButton = CType(e.Row.FindControl("lnkbtnDelete"), LinkButton)
            Dim chkItem As CheckBox = CType(e.Row.FindControl("chkItem"), CheckBox)
            Dim hplTitle As HyperLink = CType(e.Row.FindControl("hplTitle"), HyperLink)
            hplTitle.NavigateUrl = MainDomain & "van-ban-phap-luat/chi-tiet-" & grdShowNghiDinh.DataKeys(e.Row.RowIndex)("Id").ToString & ".html"


            hplTitle.Text = grdShowNghiDinh.DataKeys(e.Row.RowIndex)("TrichYeu").ToString
        End If
    End Sub

    Protected Sub grdShowThongTu_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdShowThongTu.RowDataBound
        If e.Row.RowIndex >= 0 Then
            Dim lblSTT As Label = CType(e.Row.FindControl("lblSTT"), Label)
            lblSTT.Text = CInt((hidCur_Page.Value) - 1).ToString + e.Row.RowIndex + 1
            Dim status As Integer = grdShowThongTu.DataKeys(e.Row.RowIndex)("StatusId")
            Dim lnkbtnDelete As LinkButton = CType(e.Row.FindControl("lnkbtnDelete"), LinkButton)
            Dim chkItem As CheckBox = CType(e.Row.FindControl("chkItem"), CheckBox)
            Dim hplTitle As HyperLink = CType(e.Row.FindControl("hplTitle"), HyperLink)
            hplTitle.NavigateUrl = MainDomain & "van-ban-phap-luat/chi-tiet-" & grdShowThongTu.DataKeys(e.Row.RowIndex)("Id").ToString & ".html"


            hplTitle.Text = grdShowThongTu.DataKeys(e.Row.RowIndex)("TrichYeu").ToString
        End If
    End Sub

    Protected Sub grdShowQuyetDinh_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdShowQuyetDinh.RowDataBound
        If e.Row.RowIndex >= 0 Then
            Dim lblSTT As Label = CType(e.Row.FindControl("lblSTT"), Label)
            lblSTT.Text = CInt((hidCur_Page.Value) - 1).ToString + e.Row.RowIndex + 1
            Dim status As Integer = grdShowQuyetDinh.DataKeys(e.Row.RowIndex)("StatusId")
            Dim lnkbtnDelete As LinkButton = CType(e.Row.FindControl("lnkbtnDelete"), LinkButton)
            Dim chkItem As CheckBox = CType(e.Row.FindControl("chkItem"), CheckBox)
            Dim hplTitle As HyperLink = CType(e.Row.FindControl("hplTitle"), HyperLink)
            hplTitle.NavigateUrl = MainDomain & "van-ban-phap-luat/chi-tiet-" & grdShowQuyetDinh.DataKeys(e.Row.RowIndex)("Id").ToString & ".html"


            hplTitle.Text = grdShowQuyetDinh.DataKeys(e.Row.RowIndex)("TrichYeu").ToString
        End If
    End Sub

    Protected Sub grdShowVanBanLienQuan_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdShowVanBanLienQuan.RowDataBound
        If e.Row.RowIndex >= 0 Then
            Dim lblSTT As Label = CType(e.Row.FindControl("lblSTT"), Label)
            lblSTT.Text = CInt((hidCur_Page.Value) - 1).ToString + e.Row.RowIndex + 1
            Dim status As Integer = grdShowVanBanLienQuan.DataKeys(e.Row.RowIndex)("StatusId")
            Dim lnkbtnDelete As LinkButton = CType(e.Row.FindControl("lnkbtnDelete"), LinkButton)
            Dim chkItem As CheckBox = CType(e.Row.FindControl("chkItem"), CheckBox)
            Dim hplTitle As HyperLink = CType(e.Row.FindControl("hplTitle"), HyperLink)
            hplTitle.NavigateUrl = MainDomain & "van-ban-phap-luat/chi-tiet-" & grdShowVanBanLienQuan.DataKeys(e.Row.RowIndex)("Id").ToString & ".html"


            hplTitle.Text = grdShowVanBanLienQuan.DataKeys(e.Row.RowIndex)("TrichYeu").ToString
        End If
    End Sub
End Class
